/**
 * Source file: Disability Claim Form_AutoLookups.ejs
 * Copyright (C) 2013 : Quantum Solutions
 * Id: $Id$
 * Date: $Date$
 */
 
//Turn on your selected Environment before exporting your form from TeleForm.
// serverName is also defined in ConnectWebService (should be the same value)
//var serverName = "http://127.0.0.1:50057"; // rob VS
//var serverName = "http://WIN-OV9FIKDJ4BV:88"; // Willie test
//var serverName = "http://suglifeabiqa01:88"; // Nedbank test ACBVAL
//var serverName2 = "http://suglifeabiqa01:89"; // NedBank test Suburb
var serverName = "Http://2008-4.quantumsolutions.co.za:91"; // Quantum Cloud Test acbval Autocompletes
var serverName2 = "http://2008-4.quantumsolutions.co.za:92";// Quantum Cloud Test Suburb AutoCompletes 

$(document).ready(function() {

/*
Disability Claim Form Autocomplete Field List
f182 Claimant Residential Address Suburb
f192 Claimant Residential Address Town
f215 Claimant Residential Address PostalCode

f290 Claimant Postal Address Suburb
f299 Claimant Postal Address Town
f306 Claimant Postal Address PostalCode

f147 Other Occupations
f545 Occupation at time of Disability

f199 Employer Suburb
f214 Employer Town
f222 Employer Postalcode

f450 GM Postal address Suburb
f463 GM Postal address Town
f471 GM Postal address Code

f197 Hospital Suburb
f212 Hospital Town
f220 Hospital PostalCode

*/
	// the field id is generated by Electric paper and should be changed to the apporiate value 
	// if the form is changed (fields added or deleted) this id could also change
	// the best way to locate the id is to right click on the field in firefox and select "Inspect Element"

//Other Occupations
$("#f147").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Occupation"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		// no select function
		});
		
//Ocupation at time of Diability
$("#f545").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Occupation"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		// no select function
		});

//Hospital Address Details
$("#f212").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f197").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f212').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f220').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f220").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f212').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f197').value = $.trim(pieces[pieces.length-1]); }		
		});		

//Employer Address Details
$("#f214").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f199").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f214').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f222').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f222").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f214').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f199').value = $.trim(pieces[pieces.length-1]); }		
		});		

//GM Address Details
$("#f463").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f450").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f463').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f471').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f471").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f463').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f450').value = $.trim(pieces[pieces.length-1]); }		
		});	
		
//Residential Address Details
$("#f192").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f182").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f192').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f215').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f215").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f192').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f182').value = $.trim(pieces[pieces.length-1]); }		
		});		

//Postal Address Details
$("#f299").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f290").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f299').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f306').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f306").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f299').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f290').value = $.trim(pieces[pieces.length-1]); }		
		});		
});